package com.bristua.mall.user.biz;

import com.bristua.mall.common.biz.ValidateCodeBiz;
import com.bristua.mall.common.enums.CommonCode;
import com.bristua.mall.common.exception.BizException;
import com.bristua.mall.common.utils.CacheUtil;
import com.bristua.mall.user.dao.TUserMapper;
import com.bristua.mall.user.model.TUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * Created by Administrator on 2019/4/28.
 */
@Service
@Transactional
public class UserPhoneBindBiz {

    @Autowired
    private ValidateCodeBiz validateCodeBiz;

    @Autowired
    private CacheUtil cacheUtil;

    @Resource
    private TUserMapper tUserMapper;

    @Autowired
    private UpdateUserInviteCodeBiz updateUserInviteCodeBiz;


    public void userPhoneBind(String phone,String code,String inviteCode){

        String s = validateCodeBiz.validateCode(phone, code);//验证验证码

        if("0".equals(s)){
            TUser userSession = (TUser)cacheUtil.getUserSession();
            if(!StringUtils.isEmpty(inviteCode) && !"0".equals(inviteCode)){
                updateUserInviteCodeBiz.updateUserInviteCodeBiz(inviteCode);
            }
            TUser tUser = new TUser();
            tUser.setUserId(userSession.getUserId());
            tUser.setPhone(phone);
            tUserMapper.updateByPrimaryKeySelective(tUser);
            validateCodeBiz.udpateCodeStatus(phone,code);
        }else{
            throw new BizException(CommonCode.VALIDATE_CODE_EXPIRE.getCode(),CommonCode.VALIDATE_CODE_EXPIRE.getMsg());
        }
    }
}
